/**
 *
 * The MIT License
 *
 * Copyright (c) 2009 Noritaka Horio All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Noritaka Horio 
 * @copyright Copyright (c) 2009 Noritaka Horio All rights reserved.
 * @license The MIT License
 * @version $Rev: 185 $ $Date: 2009-11-07 12:22:46 +0000 (Sat, 07 Nov 2009) $
 * @see http://mootools.net
 * 
 */

var MinutesPicker = {

	values: ["10","20","30","40","50","60"],

	initialize: function() {
		this.container = $("minutesPicker");
		this.element = $("minutes");
		this.ok = $(this.container).getElement("a.ok");
		this.cancel = $(this.container).getElement("a.cancel");
		this.title = $(this.container).getElement("span.title");
		this.initializeSelecter();
		this.bindEvents();
	},

	initializeSelecter: function() {
		this.values.each(function(v,k){
			var a  = new Element("a", {"html": v, "href": "#" + v});
			var li = new Element("li");
			a.inject(li);
			li.inject(this.element);
		}.bind(this));
		this.minutes = $(this.element).getElements("a");
	},

	bindEvents: function() {
		var buttons = this.minutes;
		buttons.each(function(element,k){
			element.addEvent("click", this.onClickMinutes.bind(this, [element,k]));
		}.bind(this));
		this.ok.addEvent("click", this.onClickOK.bind(this));
		this.cancel.addEvent("click", this.onClickCancel.bind(this));
	},

	onClickOK: function() {
		nativeWindow.close();
	},
	
	onClickCancel: function() {
		nativeWindow.close();
	},

	onClickMinutes: function(element,index){
		var buttons = this.minutes;
		buttons.each(function(v,k){
			v.removeClass("selected");
		});
		this.setMinutes(this.values[index]);
	},

	setMinutes: function(value) {
		var index = this.values.indexOf(value);
		this.minutes[index].addClass("selected");
		var minut = $(this.element).getElement("a.selected");
		this.title.set("html", minut.get("html") + "分");
	},

	getMinutes: function() {
		var minut = $(this.element).getElement("a.selected");
		return minut.get("html");
	}
}

window.addEvent("domready", MinutesPicker.initialize.bind(MinutesPicker));